# -*- coding: utf-8 -*-
"""
# @Time    : 2021/1/5 下午2:32
# @Author  : tgy
"""

import os

from os import getcwd

x = 10


def test01():
    ...
    x = 20

    def test02():
        nonlocal x
        x = 20
        print(id(x))

    test02()
    print(id(x))


def test02():
    ...

    def inner_test():
        ...
        c = 30
        # 父函数的变量a要在inner_test函数调用之前定义
        print(c, a)

    a = 20
    inner_test()


def test03(opt=None):
    ...
    # 由下面赋值语句可知，x被定义在局部命名空间中,当为x赋值时，发现 x+1 要去获取x的值，这是查找发现局部命名空间的x没有初始化，报错
    # x = x + 1
    y = x + 10

    # locals 只读
    print(locals())
    locals()['y'] = 100
    print(y)
    # print(globals())
    for key, val in globals().items():
        # print(key,val)
        print('key:{key},val:---{val}---'.format(key=key, val=val))


print(id(locals()))
print(id(globals()))
print(locals() is globals())

# if __name__ == '__main__':
#     ...
#     # test01()
#     # print(id(x))
#     # print = 222
#     print(__file__)
#
#     # globals 是可读可写
#     globals()['x'] = 299
#     test03()
